//
//  UINavigationController+Extension.swift
//  Library_y
//
//  Created by JC on 2023/6/6.
//

import UIKit
import Foundation

public extension UINavigationController {
    
    /// 设置导航栏背景色
    ///
    /// - parameter color: 背景色
    func setBarBackgroundColor(with color: UIColor) {
        self.navigationBar.barTintColor = color
        self.navigationBar.isTranslucent = false
        //        self.navigationBar.barTintColor = color
        //        self.navigationBar.setBackgroundImage(UIImage(color: color), for: UIBarMetrics.default)
        //        // 当设置为透明后，需要重新设置为默认值以恢复不透明
        //        self.navigationBar.shadowImage = UIImage()
        //        self.navigationBar.barStyle = UIBarStyle.default
    }
    
    /// 设置导航栏透明颜色
    ///
    /// - parameter color: 颜色
    func setBarTranslucentBackgroundColor(with color: UIColor) {
        self.navigationBar.setBackgroundImage(UIImage(color: color), for: UIBarMetrics.default)
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.barStyle = UIBarStyle.blackTranslucent
    }
    
    /// 设置导航栏白色透明
    func setBarTranslucent(color: UIColor = .clear) {
        self.setBarTranslucentBackgroundColor(with: color)
    }
}

public extension UINavigationController {
    
    /// 设置标题颜色
    ///
    /// - parameter color: 标题颜色
    func setTitleColor(_ color: UIColor) {
        var titleTextAttributes = navigationBar.titleTextAttributes ?? [NSAttributedString.Key: Any]()
        titleTextAttributes[NSAttributedString.Key.foregroundColor] = color
        self.navigationBar.titleTextAttributes = titleTextAttributes
    }
    
    /// 设置标题字体
    ///
    /// - parameter font: 标题字体
    func setTitleFont(_ font: UIFont) {
        var titleTextAttributes = navigationBar.titleTextAttributes ?? [NSAttributedString.Key: Any]()
        titleTextAttributes[NSAttributedString.Key.font] = font
        self.navigationBar.titleTextAttributes = titleTextAttributes
    }
    
    /// 设置标题风格
    ///
    /// - parameter color: 标题颜色
    /// - parameter font:  标题字体
    func setTitleStyle(_ color: UIColor, _ font: UIFont) {
        var titleTextAttributes = navigationBar.titleTextAttributes ?? [NSAttributedString.Key: Any]()
        titleTextAttributes[NSAttributedString.Key.foregroundColor] = color
        titleTextAttributes[NSAttributedString.Key.font] = font
        self.navigationBar.titleTextAttributes = titleTextAttributes
    }
}

public extension UINavigationController {
    
    /// storyboard,push方法
    func push<T: UIViewController>(sbName: String, vcID: T.Type) {
        let vc = UIStoryboard(name: sbName, bundle: nil).instantiateViewController(withIdentifier: "\(vcID.self)")
        pushViewController(vc, animated: true)
    }
}

extension UINavigationController {
    
    /// 自定义导航栏样式
    public func setConfigNetNavBar(_ bool: Bool = true) {
        setBarBackgroundColor(with: bool ? UIColor(hex: "#15A0E9") : .white)
        if bool {
            navigationBar.setBarBottomLine(color:  UIColor(hex: "#15A0E9"))
        } else {
            navigationBar.setBarBottomLine(color: Color.hex_9)
        }
        setTitleStyle(bool ? .white : Color.hex_3, Font.dp(18))
    }
}
